import {Component} from '@angular/core';
import { Storage } from '@ionic/storage';

import {
  ModalController,
  ViewController,
  AlertController,
  Platform
} from 'ionic-angular';

import { Common } from '../../../providers/common';

import _ from 'lodash';

@Component({
  selector: 'modal-ipm-coin-filter',
  templateUrl: './filter.html'
})
export class IpmCoinFilterModal {
	moa_page_name: any = "modal-ipm-coin-filter";
  moa_page_explain: any = " 货币基金试算- 数据过滤";

  condition = null;

  defaultEmpByTree = [];

  //可以选择的最大日期
  maxDate = "";
  //结束日期可以选择的最小日期
  minEndDate = "1970-01-01";

  constructor(
              public modalCtrl: ModalController,
              public viewCtrl: ViewController,
              public storage: Storage,
              public alertCtrl: AlertController,
              public plt: Platform,
              public common: Common) {
    this.maxDate = _.toString(parseInt(this.common.getNowFormatDate().substr(0, 4)) + 100) + "-12-31";
    this.initCondition();

  }

  cancel() {
    this.viewCtrl.dismiss("close");
  }

  ionChange(){
      //设置结束日期的最小可选日期等于起始日期
      this.minEndDate = this.condition.startdate;
  }

  /**
   * 保存搜索条件并关闭Modal返回List页面加载搜索结果
   */
  search() {

    if (this.condition.isSearchByDate && !this.condition.startDate) {
      let alert = this.alertCtrl.create({
        title: '提示',
        subTitle: '开始到期日不能空',
        "buttons": ['确认']
      });
      alert.present();
      return;
    }
    if (this.condition.isSearchByDate && !this.condition.endDate) {
      let alert = this.alertCtrl.create({
        title: '提示',
        subTitle: '结束到期日不能空',
        "buttons": ['确认']
      });
      alert.present();
      return;
    }
    if (this.condition.isSearchByDate && this.condition.startDate > this.condition.endDate) {
      let alert = this.alertCtrl.create({
        title: '提示',
        subTitle: '结束到期日不能小于开始到期日',
        "buttons": ['确认']
      });
      alert.present();
      return;
    }
    this.viewCtrl.dismiss(this.condition);
  }

  /**
   * 清空查询条件
   */
  clear() {
      this.initCondition();
  }

  /**
   * 初始化查询条件
   */
  initCondition() {
    this.condition = this.viewCtrl.getNavParams().get("filterCondition");
    this.minEndDate = this.condition.startDate;
  }

}


